package com.numericme.locale.switcher.tools;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.widget.RemoteViews;

import com.numericme.locale.switcher.QuickActionLauncher;
import com.numericme.locale.switcher.R;


public class Tools {	  
	  
	  public static final String PACKAGE_NAME = "com.numericme.locale.switcher";
	  
	  public static final String SHARED_PREFS_NAME = PACKAGE_NAME+".prefs";

	  /*
	   * 1 : 1st level tracking
	   * 0 : tracking is off
	  */
	  public static final int APP_DEBUG = 0;
	  	  
	  public static final int LOCALE_SWITCHER_NOTIFICATION_ID = 662630;	 

	  //Preference keys
	  public static final String LOCALE_SWITCHER_KEY = "locale_switcher";
	  public static final String ROTATION_KEY = "rotation";
	  public static final String KEEP_SCREEN_ON_KEY = "keep_screen_on";
	  public static final String WIFI_KEY = "wifi";
	  public static final String DISPLAY_KEY = "display";
	  public static final String SOUND_KEY = "sound";
	  public static final String SYSTEM_SETTINGS_KEY = "system_settings";
	  public static final String INSTANT_TOOLS_PREFERENCES_KEY = "instant_tools_preferences";
	  
	  public static final String INVISIBLE_NOTIFICATIONS_KEY = "invisible_notifications";
	  public static final String AUTO_ACTIVATE_ON_BOOT_KEY = "auto_activate_on_boot";

	  public static final String MAIN_PREF_SCREEN = "main_pref_screen";
	  public static final String ABOUT_PREF_SCREEN = "about_pref_screen";
	  
	  public static final String TOOLS_SETTINGS_CAT = "tools_settings_cat";
	  public static final String NOTIFICATIONS_SETTINGS_CAT = "notifications_settings_cat";

	  public static final String SETTINGSACTIVITY_INIT = "settingsactivity_init_settings";
	  public static final String MAIN_ACTIVITY_CREATE = "main_activity_init_settings";
	  
	  public static final String LIST_PREF_PREFIX = "list_pref_";

	  public static final String PREF_CHANGE_LOG = "pref_change_log";		  

	  public static final String LAST_START_VERSION_FLAG_ID = "last_start_version_flag";  
	  
	  public static String getVersionName(Context context, Class cls) 
	  {
		  try {
			  ComponentName comp = new ComponentName(context, cls);
			  PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
			  return pinfo.versionName;
		  } catch (android.content.pm.PackageManager.NameNotFoundException e) {
			  return null;
		  }
	  }

	  public static void cancelNotifications(Context context0){
		  String ns = Context.NOTIFICATION_SERVICE;
		  NotificationManager mNotificationManager = (NotificationManager) context0.getSystemService(ns);
		  mNotificationManager.cancel(Tools.LOCALE_SWITCHER_NOTIFICATION_ID);
	  }

	  public static void displayNotification(Context context0, boolean isNotificationsInvisible){
		  int notificationsIcon=0;
		  String ns = Context.NOTIFICATION_SERVICE;
		  NotificationManager mNotificationManager = (NotificationManager) context0.getSystemService(ns);

		  CharSequence tickerText = context0.getString(R.string.notifications_title);;
		  long when = System.currentTimeMillis();

		  if(!isNotificationsInvisible) {
			  notificationsIcon = R.drawable.status_bar_icon;
		  }
		  else {
			  notificationsIcon = R.drawable.no_status_bar_icon;
		  }

		  Notification notification = new Notification(notificationsIcon, tickerText, when);

		  Context context = context0.getApplicationContext();
		  
		  RemoteViews contentView = new RemoteViews(context0.getPackageName(), R.xml.custom_notification_layout);        
		  contentView.setImageViewResource(R.id.image, R.drawable.icon);
		  contentView.setTextViewText(R.id.text1, context0.getString(R.string.app_name));
		  contentView.setTextViewText(R.id.text2, context0.getString(R.string.notifications_text) + " ");
		  notification.contentView = contentView;
		  Intent notificationIntent = new Intent(context0,QuickActionLauncher.class);
		  notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		  PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
		  notification.contentIntent = contentIntent;

		  notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
		  mNotificationManager.notify(Tools.LOCALE_SWITCHER_NOTIFICATION_ID, notification);
	  }
}
